package top.lixunda.ecommerce.server.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.user.api.controller.IPermissionController;
import top.lixunda.ecommerce.server.user.api.entity.db.PermissionDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.PermissionChangeDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.PermissionInsertDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.PermissionQueryDTO;
import top.lixunda.ecommerce.server.user.service.IPermissionService;

import java.util.List;

/**
 * 权限管理接口
 *
 * @author Lixunda
 * @since 2020-04-24
 */
@Slf4j
@RestController
public class PermissionController extends BaseController implements IPermissionController {

    private final IPermissionService permissionService;

    @Autowired
    public PermissionController(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    /**
     * 添加单独的权限，超级管理员调用
     *
     * @param insertList 权限添加数据DTO
     * @return 添加成功的权限信息
     */
    @Override
    public MessageObject<List<PermissionDO>> insertPermission(List<PermissionInsertDTO> insertList) {
        List<PermissionDO> result = permissionService.insertPermission(insertList);
        return getSuccessMessageObject("添加权限成功！", result);
    }

    /**
     * 根据权限编号修改权限信息，超级管理员调用
     *
     * @param changeList 权限修改数据DTO
     * @return 修改后的权限信息
     */
    @Override
    public MessageObject<List<PermissionDO>> changePermissionById(List<PermissionChangeDTO> changeList) {
        List<PermissionDO> result = permissionService.changePermissionById(changeList);
        return getSuccessMessageObject("修改权限成功！", result);
    }

    /**
     * 根据权限编号删除权限信息，超级管理员调用
     *
     * @param permissionIdList 权限编号列表
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removePermission(List<Integer> permissionIdList) {
        Boolean result = permissionService.removePermission(permissionIdList);
        return getSuccessMessageObject("删除权限成功！", result);
    }

    /**
     * 根据权限编号查询单个权限信息，超级管理员调用
     *
     * @param id 权限编号
     * @return 单个权限信息
     */
    @Override
    public MessageObject<PermissionDO> queryPermissionById(Integer id) {
        PermissionDO result = permissionService.queryPermissionById(id);
        return getSuccessMessageObject("查询权限成功！", result);
    }

    /**
     * 根据多个权限编号查询多个权限信息，超级管理员调用
     *
     * @param ids 权限编号列表
     * @return 权限信息列表
     */
    @Override
    public MessageObject<List<PermissionDO>> queryPermissionByIds(List<Integer> ids) {
        List<PermissionDO> result = permissionService.queryPermissionByIds(ids);
        return getSuccessMessageObject("查询权限成功！", result);
    }

    /**
     * 条件分页查询权限数据，超级管理员调用
     *
     * @param pageDTO 分页条件DTO
     * @return 分页数据
     */
    @Override
    public MessageObject<Page<PermissionDO>> queryPermissionPageWrapper(
            @XumValid PageDTO<PermissionDO, PermissionQueryDTO> pageDTO) {
        Page<PermissionDO> result = (Page<PermissionDO>) permissionService.queryPermissionPageWrapper(pageDTO);
        return getSuccessMessageObject("查询权限成功！", result);
    }
}
